package com.example.demo.mvc;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.example.demo.entity.Student;
import com.example.demo.service.StudentService;


@Controller
@RequestMapping("/getStudents")
public class StudentAction {
	
	@Autowired
	private StudentService studentServiceimpl;
	private List<Student> studentList1;
	private int eb;
	//@Autowired
	//private FileUploaderAndDownloaderutil fileUploaderAndDownloaderutil;
	//private String fileName;
    private Student student1;
    
	@RequestMapping("/insertStudent")
	public ModelAndView insertStudent(@ModelAttribute("student") Student student){
		ModelAndView modelAndView= new ModelAndView();
		eb= studentServiceimpl.addStudent(student);
		if(eb==1){
			modelAndView.setViewName("selectStudent");
			return modelAndView;
		}
		modelAndView.setViewName("error");
		modelAndView.addObject("erormes","抱歉,添加失败");
		return modelAndView;
	}
	
	
	@RequestMapping("/deleteStudent")
	public ModelAndView deleteStudent(@RequestParam("sid") Integer sid){
		ModelAndView modelAndView=  new ModelAndView();
		eb=studentServiceimpl.delStudent(sid);
		if(eb==1){
			modelAndView.setViewName("selectStudent");
			return modelAndView;
		}
		modelAndView.setViewName("error");
		modelAndView.addObject("errormes","抱歉,删除失败");
		return modelAndView;
	}
	
	@RequestMapping("/updateStudent")
	public ModelAndView updateStudent(@ModelAttribute("student") Student student){
		ModelAndView modelAndView= new ModelAndView();
		eb= studentServiceimpl.updateStudent(student);
		if(eb==1){
			modelAndView.setViewName("selectStudent");
			return modelAndView;
		}
		modelAndView.setViewName("error");
		modelAndView.addObject("erormes","抱歉,修改失败");
		return modelAndView;
	}
	
	@RequestMapping("/selectStudent")
	public ModelAndView selectStudent(){
		studentList1=new ArrayList<Student>();
		studentList1= studentServiceimpl.selectStudent();
		ModelAndView modelAndView= new ModelAndView();
		modelAndView.setViewName("selectStudent");
		modelAndView.addObject("studentList1",studentList1);
		return modelAndView;
	}
	
	@RequestMapping("/selectOneStudent/{sid}")
	public ModelAndView selectOneStudent(@PathVariable("sid") Integer sid){
		student1=new Student();
		student1= studentServiceimpl.selectOneStudent(sid);
		ModelAndView modelAndView= new ModelAndView();
		modelAndView.setViewName("selectOneStudent");
		modelAndView.addObject("student1",student1);
		return modelAndView;
	}
	
	/*@RequestMapping("/upfile")
	@ResponseBody
	public void Uploader(MultipartFile file){
		fileUploaderAndDownloaderutil.upload(file);
	}
	
	@RequestMapping("/downfile")
	public void Downloader(HttpServletRequest request,HttpServletResponse response){
		fileName="wenjian";
		fileUploaderAndDownloaderutil.download(request, response, fileName);
	}*/
	
	@RequestMapping("/insertStudent1")
	public String insertStudent2(Student student){
		return "/insertStudent1";
	}
}
